package cn.fl.capital.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import cmm.mid.core.framework.page.KeyValueVO;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.comm.facade.framework.dto.SysDictionaryDTO;
import cmm.mid.core.framework.page.Pager;
import cmm.user.facade.system.api.ISysOrgApiService;
import cmm.user.facade.system.dto.SysOrgDTO;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.DateUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.capital.condition.CapOverdueInfoQueryCondition;
import cn.fl.capital.constant.CapitalConstant;
import cn.fl.capital.dto.CapOverdueInfoDTO;
import cn.fl.capital.service.ICapOverdueInfoApiService;
import cn.fl.capital.service.ICapOverdueInfoService;
import cn.fl.capital.transform.CapOverdueInfoVOTransform;
import cn.fl.capital.vo.CapOverdueInfoVO;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.framework.constant.DictionaryConstant;
import cn.fl.mcr.api.IMcrBillMApiService;
import cn.fl.mcr.condition.McrBillMQC;
import cn.fl.mcr.constant.McrBillMConstant;
import cn.fl.mcr.dto.McrBillMDTO;
import cn.fl.project.condition.PrjBscInfoQueryCondition;
import cn.fl.project.condition.PrjPrdDtlQueryCondition;
import cn.fl.project.constant.PrjPrdDtlConstant;
import cn.fl.project.dto.PrjBscInfoDTO;
import cn.fl.project.dto.PrjPrdDtlDTO;
import cn.fl.project.service.IProjectApiService;
import cn.fl.ref.fribaseinfo.vo.FriBaseInfoVO;
import cn.fl.risk.condition.RskAppoRecQueryCondition;
import cn.fl.risk.dto.RskAppoRecDTO;
import cn.fl.risk.service.IRskAppoRecApiService;
import com.alibaba.fastjson.JSONObject;
import fl.customer.facade.customer.api.ICustomerQueryApiService;
import fl.customer.facade.customer.dto.CstContactInfoDTO;
import fl.ref.fribaseinfo.condition.FriBaseInfoQueryCondition;
import fl.ref.fribaseinfo.constant.FriBaseInfoConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @Description: 逾期信息表 客户端服务层接口实现
 * 业务层方法名称前缀：
 * 新增：add*
 * 删除：remove*
 * 修改：modify*
 * 批量：batch*
 * 查询对象：select* get*
 * 查询集合：search*
 */
@Service
public class CapOverdueInfoServiceImpl implements ICapOverdueInfoService {

    /**
     * 日志
     */
    private static final Logger logger = LoggerFactory.getLogger(CapOverdueInfoServiceImpl.class);

    /**
     * 自定义 API 暴露服务
     */
    @Autowired
    private ICapOverdueInfoApiService capOverdueInfoApiService;

    /**
     * 数据字典 服务层BEAN
     */
    @Autowired
    private ISysDictionaryApiService sysDictionaryApiService;
    @Autowired
    private ISysOrgApiService sysOrgApiService;
    @Autowired
    private IProjectApiService projectApiService;
    @Autowired
    private IRskAppoRecApiService rskAppoRecApiService;
    @Autowired
    private ICustomerQueryApiService customerQueryApiService;

    @Autowired
    private ISysDictionaryApiService sysDictionaryService;

    @Override
    public CapOverdueInfoVO selectById(Long id) throws BaseException {
        if (StringUtils.isEmpty(id)) return null;
        CapOverdueInfoDTO dto = this.capOverdueInfoApiService.selectCapOverdueInfoById(id);
        return CapOverdueInfoVOTransform.toVO(dto);
    }

    @Override
    public Long add(CapOverdueInfoVO entity) throws BaseException {
        if (null == entity) return null;
        return this.capOverdueInfoApiService.addCapOverdueInfo(CapOverdueInfoVOTransform.toDTO(entity));
    }

    @Override
    public boolean removeByIds(List<Long> ids) throws BaseException {
        if (CollectionUtils.isEmpty(ids)) return false;
        return this.capOverdueInfoApiService.removeCapOverdueInfoByIds(ids, CurrentThreadContext.getCurrentUserId());
    }

    @Override
    public boolean modify(CapOverdueInfoVO entity) throws BaseException {
        if (null == entity) return false;
        return this.capOverdueInfoApiService.modifyCapOverdueInfo(CapOverdueInfoVOTransform.toDTO(entity));
    }

    @Override
    public Pager searchListPage(Pager page) throws BaseException {
        if (null == page) page = new Pager();
        page = this.capOverdueInfoApiService.searchCapOverdueInfoListPage(page, CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
        if (CollectionUtils.isNotEmpty(page.getResultList())) {
            page.setResultList(CapOverdueInfoVOTransform.toVOList((List<CapOverdueInfoDTO>) page.getResultList()));
        }
        return page;
    }

    /*******************************
     * 查询客户逾期支付表数据
     * @param condition
     * @return
     * @throws BaseException
     *******************************/
    @Override
    public List<CapOverdueInfoVO> searchOvdPayList(CapOverdueInfoQueryCondition condition) throws BaseException {
        List<CapOverdueInfoVO> voList = CapOverdueInfoVOTransform.toVOList(this.capOverdueInfoApiService.searchOvdPayList(condition));
        if (CollectionUtils.isNotEmpty(voList)) {
            List<SysOrgDTO> orgList = sysOrgApiService.searchListByOrgTypCd(CurrentThreadContext.getCurrentSysCd());
            if (CollectionUtils.isNotEmpty(orgList)) {
                Map<String, String> orgMap = new HashMap<>(orgList.size());
                for (SysOrgDTO org : orgList) {
                    orgMap.put(org.getOrgCd(), org.getOrgNm());
                }

                for (CapOverdueInfoVO vo : voList) {
                    if (orgMap.containsKey(vo.getBuOrgCd())) {
                        vo.setBuOrgCdNm(orgMap.get(vo.getBuOrgCd()));
                    }
                }
            }
        }
        return voList;
    }

    /*******************************
     * 查询客户支付表逾期明细
     * @param condition
     * @return
     * @throws BaseException
     *******************************/
    @Override
    public List<CapOverdueInfoVO> searchOvdDtlList(CapOverdueInfoQueryCondition condition) throws BaseException {
        List<CapOverdueInfoVO> voList = CapOverdueInfoVOTransform.toVOList(this.capOverdueInfoApiService.searchOvdDtlList(condition));
        return voList;
    }

    /*******************************
     * 查询客户支付表逾期明细 分页
     * @param page
     * @return
     * @throws BaseException
     *******************************/
    @Override
    public Pager searchOvdDtlListPage(Pager page) throws BaseException {
        if (null == page) page = new Pager();
        page = this.capOverdueInfoApiService.searchOvdDtlListPage(page);
        if (CollectionUtils.isNotEmpty(page.getResultList())) {
            List<CapOverdueInfoVO> voList = CapOverdueInfoVOTransform.toVOList((List<CapOverdueInfoDTO>) page.getResultList());
            if (CollectionUtils.isNotEmpty(voList) && voList.get(0).getTotalAmount() > 0) {
                Long totalAmt = voList.get(0).getTotalAmount();
                for (int i = voList.size() - 1; i >= 0; i--) {
                    CapOverdueInfoVO vo = voList.get(i);
                    if (vo.getPenaltyAmount() - vo.getReduAmount() >= totalAmt) {
                        vo.setActualAmount(totalAmt);
                        vo.setPenaltyAmount(vo.getPenaltyAmount() - vo.getReduAmount() - totalAmt);
                        break;
                    } else {
                        vo.setActualAmount(totalAmt + vo.getReduAmount() - vo.getPenaltyAmount());
                        vo.setDataStatusCd("0");
                        vo.setPenaltyAmount(0L);//罚息已核销完
                        totalAmt -= vo.getActualAmount();
                    }
                }
            }

            page.setResultList(voList);
        }
        return page;
    }

    /*******************************
     * 减免罚息，查询客户支付表逾期明细
     * @param condition
     * @return
     * @throws BaseException
     *******************************/
    public List<CapOverdueInfoVO> searchPayDtlList(CapOverdueInfoQueryCondition condition) throws BaseException {
        List<CapOverdueInfoVO> voList = CapOverdueInfoVOTransform.toVOList(this.capOverdueInfoApiService.searchOvdDtlList(condition));
        if (CollectionUtils.isNotEmpty(voList)) {
            Long totalAmt = voList.get(0).getTotalAmount();//累计核销罚息金额
            for (int i = voList.size() - 1; i >= 0; i--) {
                CapOverdueInfoVO vo = voList.get(i);
                vo.setPenaltyAmount(vo.getPenaltyAmount() - vo.getReduAmount());
                if (vo.getTotalAmount().longValue() >= totalAmt) {
                    vo.setDataStatusCd("1");//可减免
                    if (vo.getPenaltyAmount() == (vo.getPayAmount() + vo.getReduAmount())) {
                        vo.setDataStatusCd("0");//不可减免
                        //voList.remove(i);//不可减免
                    }
                } else {
                    //vo.setDataStatusCd("0");//不可减免
                    voList.remove(i);//不可减免
                }
                /*if(vo.getPenaltyAmount()-vo.getReduAmount()>=totalAmt){
                    vo.setActualAmount(totalAmt);
                    vo.setPenaltyAmount(vo.getPenaltyAmount()-vo.getReduAmount()-totalAmt);
                    vo.setDataStatusCd("1");//可减免
                }else{
                    //vo.setActualAmount(totalAmt+vo.getReduAmount()-vo.getPenaltyAmount());
                    vo.setActualAmount(vo.getPenaltyAmount()-vo.getReduAmount());
                    vo.setDataStatusCd("0");//不可减免
                    vo.setPenaltyAmount(vo.getPenaltyAmount()-vo.getReduAmount());//罚息已核销完
                    totalAmt-=vo.getActualAmount();
                }*/
            }
        }
        return voList;
    }

    @Override
    public List<CapOverdueInfoVO> searchList(CapOverdueInfoQueryCondition condition) throws BaseException {
        return CapOverdueInfoVOTransform.toVOList(this.capOverdueInfoApiService.searchCapOverdueInfoList(condition));
    }

    /**
     * 导出逾期查询
     *
     * @param condition
     * @return
     */
    public List<CapOverdueInfoVO> searchOverdueList(CapOverdueInfoQueryCondition condition) throws BaseException {
        condition.setOrgCd(CurrentThreadContext.getCurrentOrgCd());
        condition.setSysCd(CurrentThreadContext.getCurrentSysCd());
        condition.setUsrId(CurrentThreadContext.getCurrentUserId());
        return CapOverdueInfoVOTransform.toVOList(this.capOverdueInfoApiService.searchOverdueList(condition));
    }

    /**
     * 逾期明细查询
     *
     * @param condition
     * @return
     * @throws BaseException
     */
    public List<CapOverdueInfoVO> searchDtlList(CapOverdueInfoQueryCondition condition) throws BaseException {
        return CapOverdueInfoVOTransform.toVOList(this.capOverdueInfoApiService.searchDtlList(condition));
    }

    /*******************************
     * 逾期催收，查询客户支付表逾期明细（支付表状态+预约时间）
     * @param condition
     * @return
     * @throws BaseException
     *******************************/
    @Override
    public List<CapOverdueInfoVO> searchOvdPayDtlList(CapOverdueInfoQueryCondition condition) throws BaseException {
        condition.setSysCd(CurrentThreadContext.getCurrentSysCd());
        condition.setOrgCd(CurrentThreadContext.getCurrentOrgCd());
        condition.setUsrId(CurrentThreadContext.getCurrentUserId());
        List<CapOverdueInfoDTO> dtoList = this.capOverdueInfoApiService.searchOverdueList(condition);
        List<CapOverdueInfoVO> voList = CapOverdueInfoVOTransform.toVOList(dtoList);
        if (CollectionUtils.isNotEmpty(voList)) {
            List<Long> payIdList = new ArrayList<>(voList.size());//全部支付表ID集合
            List<Long> cstIdList = new ArrayList<>(voList.size());//客户ID集合
            List<Long> sqPayIdList = new ArrayList<>(voList.size());//我司支付表ID集合
            List<Long> prjIdList = new ArrayList<>(voList.size());//项目ID集合

            for (CapOverdueInfoVO vo : voList) {
                payIdList.add(vo.getPaySchId());
                cstIdList.add(vo.getCstId());
                if(vo.getIsSqBiz().equals("1")){// 我司业务
                    sqPayIdList.add(vo.getPaySchId());
                }
                prjIdList.add(vo.getPrjId());
            }

            //查询支付表历史最高逾期天数
            List<CapOverdueInfoDTO> ovdList = this.capOverdueInfoApiService.selectHisMaxOvdDay(payIdList);
            Map<Long, Long> ovdDayMap = null;
            if (CollectionUtils.isNotEmpty(ovdList)) {
                ovdDayMap = new HashMap<>(ovdList.size());
                for (CapOverdueInfoDTO dto : ovdList) {
                    ovdDayMap.put(dto.getPaySchId(), dto.getOverdueDay());
                }
            }

            Map<Long, PrjPrdDtlDTO> prdStsMap = null;
            //查询支付表 资产状态
            if(CollectionUtils.isNotEmpty(sqPayIdList)){
                PrjPrdDtlQueryCondition prjPrdDtlQueryCondition = new PrjPrdDtlQueryCondition();
                prjPrdDtlQueryCondition.setPaySchIdList(sqPayIdList);
                List<PrjPrdDtlDTO> prdList = projectApiService.selectPrjPrdDtlList(prjPrdDtlQueryCondition);
                if (CollectionUtils.isNotEmpty(prdList)) {
                    prdStsMap = new HashMap<>(sqPayIdList.size());
                    for (PrjPrdDtlDTO prjPrdDtlDTO : prdList) {
                        prdStsMap.put(prjPrdDtlDTO.getPaySchId(), prjPrdDtlDTO);
                    }
                }
            }

            //根据项目ID查询项目类型
            PrjBscInfoQueryCondition qc = new PrjBscInfoQueryCondition();
            qc.setIdList(prjIdList);
            List<PrjBscInfoDTO> prjBscInfoDTOS = projectApiService.searchPrjBscInfoList(qc);
            

            //查询支付表预约时间
            RskAppoRecQueryCondition appoRecQueryCondition = new RskAppoRecQueryCondition();
            appoRecQueryCondition.setPaySchIdList(payIdList);
            List<RskAppoRecDTO> appoList = rskAppoRecApiService.searchRskAppoRecList(appoRecQueryCondition);
            Map<Long, Date> appoMap = null;
            if (CollectionUtils.isNotEmpty(appoList)) {
                appoMap = new HashMap<>(appoList.size());
                for (RskAppoRecDTO recDTO : appoList) {
                    appoMap.put(recDTO.getPaySchId(), recDTO.getAppoDt());
                }
            }

            //查询联系人电话
            List<CstContactInfoDTO> contactInfoDTOList = customerQueryApiService.searchCstContactInfo(cstIdList);
            Map<Long, CstContactInfoDTO> cstMap = null;
            if (CollectionUtils.isNotEmpty(contactInfoDTOList)) {
                BaseLogUtils.info(logger, "searchOvdPayDtlList", "逾期催收，查询客户联系人==>" + contactInfoDTOList.size());
                cstMap = new HashMap<>(contactInfoDTOList.size());
                for (CstContactInfoDTO dto : contactInfoDTOList) {
                    cstMap.put(dto.getId(), dto);
                }
            } else {
                BaseLogUtils.info(logger, "searchOvdPayDtlList", "逾期催收，查询客户联系人==>返回空");
            }

            Date now = new Date();
            //资产状态
            Map<String, String> prdStsCdMap = this.sysDictionaryApiService.searchGroupToMap(CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd(), PrjPrdDtlConstant.CODE_PRJ_PRD_STS_CD);
            //需提醒资产状态
            Map<String, String> inPrdCdMap = this.sysDictionaryApiService.searchGroupToMap(CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd(), CapitalConstant.CODE_PRJ_PRD_STS_CD);
            //不显示资产状态
            Map<String,String> psCdMap = this.sysDictionaryApiService.searchGroupToMap(CurrentThreadContext.getCurrentSysCd(),CurrentThreadContext.getCurrentOrgCd(),CapitalConstant.CODE_CAP_PRD_STS_CD);
            //查询项目类型
            Map<String,String> projectTypeList = this.sysDictionaryApiService.searchGroupToMap(CurrentThreadContext.getCurrentSysCd(),CurrentThreadContext.getCurrentOrgCd(),DictionaryConstant.CODE_PRJ_TYPE_CD);
            Map<String,String> sueStsCdMap = this.sysDictionaryApiService.searchGroupToMap(CurrentThreadContext.getCurrentSysCd(),CurrentThreadContext.getCurrentOrgCd(),"SUE0007");

            List<CapOverdueInfoVO> capOverdueInfoVOList=new ArrayList<>(voList.size());
            //组合 支付表资产状态+预约时间
            for (CapOverdueInfoVO vo : voList) {
                if(sueStsCdMap!=null && sueStsCdMap.containsKey(vo.getSueStsCd())){
                    vo.setSueStsCd(sueStsCdMap.get(vo.getSueStsCd()));
                }
                vo.setIsRemind(0);//不需要提醒
                if (prdStsMap != null && prdStsMap.containsKey(vo.getPaySchId())) {
                    PrjPrdDtlDTO prjPrdDtlDTO = prdStsMap.get(vo.getPaySchId());
                    if(prjPrdDtlDTO != null){
                        vo.setPrdStsCd(prjPrdDtlDTO.getPrdStsCd());
                    }
                    if(psCdMap!=null&&psCdMap.containsKey(vo.getPrdStsCd())){
                        continue;
                    }
                    if (prdStsCdMap != null && StringUtils.isNotBlank(vo.getPrdStsCd()) && prdStsCdMap.containsKey(vo.getPrdStsCd())) {
                        vo.setPrdStsCdNm(prdStsCdMap.get(vo.getPrdStsCd()));
                    }
                    if(vo.getIsSqBiz().equals("0")){
                        vo.setPrdStsCdNm("正常");
                    }
                }
                for (PrjBscInfoDTO prjBscInfoDTO : prjBscInfoDTOS) {
                    if (Objects.equals(prjBscInfoDTO.getId(), vo.getPrjId())) {
                        //查询项目类型
                        if (projectTypeList != null && projectTypeList.containsKey(prjBscInfoDTO.getPrjTypCd())) {
                            vo.setPrjTypCd(prjBscInfoDTO.getPrjTypCd());
                            vo.setPrjTypCdNm(projectTypeList.get(prjBscInfoDTO.getPrjTypCd()));
                            vo.setPrdTypCdNm(projectTypeList.get(prjBscInfoDTO.getPrjTypCd()));
                             vo.setFundRasInsNm(prjBscInfoDTO.getInsCode());//机构编号
                            vo.setFundRasInsNm(prjBscInfoDTO.getFundRasInsNm());//机构名称
                        }
                    }
                }
                if (appoMap != null && appoMap.containsKey(vo.getPaySchId())) {
                    vo.setAppoDt(appoMap.get(vo.getPaySchId()));
                }
                if (cstMap != null && cstMap.containsKey(vo.getCstId())) {
                    CstContactInfoDTO contactInfoDTO = cstMap.get(vo.getCstId());
                    vo.setLkMan(contactInfoDTO.getCstNm());//联系人
                    vo.setLkPhone(contactInfoDTO.getMp1());//联系电话
                }
                //应收金额=罚息金额-已支付金额-减免金额
                vo.setRecAmt(vo.getOverdueAmount()+vo.getPenaltyAmount() - vo.getPayAmount() - vo.getReduAmount());
                vo.setPenaltyAmount(vo.getPenaltyAmount() - vo.getPayAmount() - vo.getReduAmount());

                //到预约时间或者未预约的需提醒
                if (DateUtils.getDaysBetweenTwoDates(vo.getAppoDt() == null ? now : vo.getAppoDt(), now) == 0) {
                    //如果资产在任何一种结清过程中时，预约提醒警示灯置灰，催收记录操作置灰
                    //结清中状态分别为：正常结清中/提前结清中/处置结清中/优车处置结清中/汽车金融处置结清中/赎回结清中/汽车金融赎回结清中/厂商回购结清/经销商回购结清
                    if (inPrdCdMap.containsKey(vo.getPrdStsCd())) {
                        vo.setIsRemind(1);
                    }
                }

                //历史最高逾期天数
                if(CollectionUtils.isMapNotEmpty(ovdDayMap)&&ovdDayMap.containsKey(vo.getPaySchId())){
                    vo.setOvdMaxDay(ovdDayMap.get(vo.getPaySchId()));
                }
                capOverdueInfoVOList.add(vo);
            }
            return capOverdueInfoVOList;
        }

        return voList;
    }


    /**
     * 逾期计算
     *
     * @param condition
     * @return
     * @throws BaseException
     */
    public boolean addOvdInfo(CapOverdueInfoQueryCondition condition) throws BaseException {
        return this.capOverdueInfoApiService.autoComputeCstOverdue(condition);
    }

    /**
     * 计算客户应付金额
     *
     * @param condition
     * @return
     * @throws BaseException
     */
    @Override
    public CapOverdueInfoVO calcCstOvdAmt(CapOverdueInfoQueryCondition condition) throws BaseException {
        if (condition == null) {
            condition = new CapOverdueInfoQueryCondition();
        }
        condition.setSysCd(CurrentThreadContext.getCurrentSysCd());
        condition.setOrgCd(CurrentThreadContext.getCurrentOrgCd());
        return CapOverdueInfoVOTransform.toVO(capOverdueInfoApiService.calcCstOvdAmt(condition));
    }

    /**
     * 添加数据字典名称
     *
     * @param voList
     */
    public void setDictData(List<CapOverdueInfoVO> voList) {
        if (CollectionUtils.isEmpty(voList)) return;
        /*for(CapOverdueInfoVO vo:voList){

        }*/
    }

    /*******************************
     * 逾期催收，查询客户支付表逾期明细（支付表状态+预约时间）
     * @param condition
     * @return
     * @throws BaseException
     * @author jiangzg
     * @date 2017-8-11 10:56:28 add
     *******************************/
    @Override
    public List<CapOverdueInfoVO> searchMyOvdPayDtlList(CapOverdueInfoQueryCondition condition) throws BaseException {
        condition.setSysCd(CurrentThreadContext.getCurrentSysCd());
        condition.setOrgCd(CurrentThreadContext.getCurrentOrgCd());
        condition.setUsrId(CurrentThreadContext.getCurrentUserId());
        // jiangzg 2017-8-16 16:04:04 add 增加资产结清状态过滤
        List<KeyValueVO> kvList = this.sysDictionaryService.getKvListAll(CapitalConstant.CODE_CAP_PRD_STS_CD, CapitalConstant.SYSCD, CapitalConstant.SYSORG);
        List<String> prdStsCdList = null;//已结清资产状态集合
        if (CollectionUtils.isNotEmpty(kvList)) {
            prdStsCdList = new ArrayList<>(kvList.size());
            for (KeyValueVO kv : kvList) {
                prdStsCdList.add(kv.getKey());
            }
        }
        condition.setExcludedPrdStsCdList(prdStsCdList);
        List<CapOverdueInfoDTO> dtoList = this.capOverdueInfoApiService.searchMyOverdueList(condition);
        List<CapOverdueInfoVO> voList = CapOverdueInfoVOTransform.toVOList(dtoList);
        if (CollectionUtils.isNotEmpty(voList)) {
            List<Long> payIdList = new ArrayList<>(voList.size());//支付表ID集合
            List<Long> cstIdList = new ArrayList<>(voList.size());//客户ID集合
            for (CapOverdueInfoVO vo : voList) {
                payIdList.add(vo.getPaySchId());
                cstIdList.add(vo.getCstId());
            }

            //查询支付表历史最高逾期天数
            List<CapOverdueInfoDTO> ovdList = this.capOverdueInfoApiService.selectHisMaxOvdDay(payIdList);
            Map<Long, Long> ovdDayMap = null;
            if (CollectionUtils.isNotEmpty(ovdList)) {
                ovdDayMap = new HashMap<>(ovdList.size());
                for (CapOverdueInfoDTO dto : ovdList) {
                    ovdDayMap.put(dto.getPaySchId(), dto.getOverdueDay());
                }
            }

            //查询支付表 资产状态
            PrjPrdDtlQueryCondition prjPrdDtlQueryCondition = new PrjPrdDtlQueryCondition();
            prjPrdDtlQueryCondition.setPaySchIdList(payIdList);
            List<PrjPrdDtlDTO> prdList = projectApiService.selectPrjPrdDtlList(prjPrdDtlQueryCondition);
            Map<Long, PrjPrdDtlDTO> prdStsMap = null;
            if (CollectionUtils.isNotEmpty(prdList)) {
                prdStsMap = new HashMap<>(payIdList.size());
                for (PrjPrdDtlDTO prjPrdDtlDTO : prdList) {
                    prdStsMap.put(prjPrdDtlDTO.getPaySchId(), prjPrdDtlDTO);
                }
            }

            //查询支付表预约时间
            RskAppoRecQueryCondition appoRecQueryCondition = new RskAppoRecQueryCondition();
            appoRecQueryCondition.setPaySchIdList(payIdList);
            List<RskAppoRecDTO> appoList = rskAppoRecApiService.searchRskAppoRecList(appoRecQueryCondition);
            Map<Long, Date> appoMap = null;
            if (CollectionUtils.isNotEmpty(appoList)) {
                appoMap = new HashMap<>(appoList.size());
                for (RskAppoRecDTO recDTO : appoList) {
                    appoMap.put(recDTO.getPaySchId(), recDTO.getAppoDt());
                }
            }

            //查询联系人电话
            List<CstContactInfoDTO> contactInfoDTOList = customerQueryApiService.searchCstContactInfo(cstIdList);
            Map<Long, CstContactInfoDTO> cstMap = null;
            if (CollectionUtils.isNotEmpty(contactInfoDTOList)) {
                BaseLogUtils.info(logger, "searchOvdPayDtlList", "逾期催收，查询客户联系人==>" + contactInfoDTOList.size());
                cstMap = new HashMap<>(contactInfoDTOList.size());
                for (CstContactInfoDTO dto : contactInfoDTOList) {
                    cstMap.put(dto.getId(), dto);
                }
            } else {
                BaseLogUtils.info(logger, "searchOvdPayDtlList", "逾期催收，查询客户联系人==>返回空");
            }

            Date now = new Date();
            //资产状态
            Map<String, String> prdStsCdMap = this.sysDictionaryApiService.searchGroupToMap(CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd(), PrjPrdDtlConstant.CODE_PRJ_PRD_STS_CD);
            //需提醒资产状态
            Map<String, String> inPrdCdMap = this.sysDictionaryApiService.searchGroupToMap(CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd(), CapitalConstant.CODE_PRJ_PRD_STS_CD);
            //不显示资产状态
            Map<String,String> psCdMap = this.sysDictionaryApiService.searchGroupToMap(CurrentThreadContext.getCurrentSysCd(),CurrentThreadContext.getCurrentOrgCd(),CapitalConstant.CODE_CAP_PRD_STS_CD);
            Map<String,String> sueCdMap = this.sysDictionaryApiService.searchGroupToMap(CurrentThreadContext.getCurrentSysCd(),CurrentThreadContext.getCurrentOrgCd(),"SUE0007");

            List<CapOverdueInfoVO> capOverdueInfoVOList=new ArrayList<>(voList.size());
            //组合 支付表资产状态+预约时间
            for (CapOverdueInfoVO vo : voList) {
                vo.setIsRemind(0);//不需要提醒
                if (prdStsMap != null && prdStsMap.containsKey(vo.getPaySchId())) {
                    vo.setPrdStsCd(prdStsMap.get(vo.getPaySchId()).getPrdStsCd());
                    if(psCdMap !=null && psCdMap.containsKey(vo.getPrdStsCd())){
                        continue;
                    }
                    if (prdStsCdMap != null && prdStsCdMap.containsKey(vo.getPrdStsCd())) {
                        vo.setPrdStsCdNm(prdStsCdMap.get(vo.getPrdStsCd()));
                    }
                }
                if (appoMap != null && appoMap.containsKey(vo.getPaySchId())) {
                    vo.setAppoDt(appoMap.get(vo.getPaySchId()));
                }
                if (cstMap != null && cstMap.containsKey(vo.getCstId())) {
                    CstContactInfoDTO contactInfoDTO = cstMap.get(vo.getCstId());
                    vo.setLkMan(contactInfoDTO.getCstNm());//联系人
                    vo.setLkPhone(contactInfoDTO.getMp1());//联系电话
                }
                if(sueCdMap != null && sueCdMap.containsKey(vo.getSueStsCd())){
                    vo.setSueStsCd(sueCdMap.get(vo.getSueStsCd()));
                }
                //应收金额=罚息金额-已支付金额-减免金额
                vo.setRecAmt(vo.getOverdueAmount()+vo.getPenaltyAmount() - vo.getPayAmount() - vo.getReduAmount());
                vo.setPenaltyAmount(vo.getPenaltyAmount() - vo.getPayAmount() - vo.getReduAmount());

                //到预约时间或者未预约的需提醒
                if (DateUtils.getDaysBetweenTwoDates(vo.getAppoDt() == null ? now : vo.getAppoDt(), now) == 0) {
                    //如果资产在任何一种结清过程中时，预约提醒警示灯置灰，催收记录操作置灰
                    //结清中状态分别为：正常结清中/提前结清中/处置结清中/优车处置结清中/汽车金融处置结清中/赎回结清中/汽车金融赎回结清中/厂商回购结清/经销商回购结清
                    if (inPrdCdMap.containsKey(vo.getPrdStsCd())) {
                        vo.setIsRemind(1);
                    }
                }

                //历史最高逾期天数
                if(CollectionUtils.isMapNotEmpty(ovdDayMap)&&ovdDayMap.containsKey(vo.getPaySchId())){
                    vo.setOvdMaxDay(ovdDayMap.get(vo.getPaySchId()));
                }
                capOverdueInfoVOList.add(vo);
            }
            return capOverdueInfoVOList;
        }

        return voList;
    }

    /**
     * 小贷逾期历史计算
     * @param condition
     * @return
     * @throws BaseException
     */
    public void calcMcrCapOverdueHis(CapOverdueInfoQueryCondition condition) throws BaseException {
        this.capOverdueInfoApiService.calcMcrCapOverdueHis(condition);
    }

}
